package com.data.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("ALL")
@Slf4j
@Component
public class KamliClientUtil {


    private static String addUserUrl;

    @Value("${kamailio.addUser:}")
    public void setAddUserUrl(String addUserUrl) {
        KamliClientUtil.addUserUrl = addUserUrl;
    }

    public static Boolean doPostJson(String uniqueNo) {

        Boolean flag = false;
        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo);

        String result = HttpClientUtil.doPostJson(addUserUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    public static Boolean doPost(String uniqueNo,String password) {

        Boolean flag = false;
        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo).replace("870830",password);

        String result = HttpClientUtil.doPostJson(addUserUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

}
